/**
 * Project Name:spring-cloud-pilot File Name:Restarter.java Package Name:org.spring.cloud.pilot
 * Date:2019年10月18日上午11:58:50 Copyright (c) 2019, dengqian37@126.com All Rights Reserved.
 *
 */

package org.spring.cloud.pilot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.cloud.pilot.util.JarTool;

import com.google.common.base.Strings;

/**
 * ClassName:Restarter <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2019年10月18日 上午11:58:50 <br/>
 * 
 * @author dengqian
 * @version
 * @since JDK 1.8
 * @see
 */
public class Restarter {

    private static final Logger LOGGER = LoggerFactory.getLogger(Restarter.class);
    
    private static final Object INSTANCE_MONITOR = new Object();

    private static Restarter instance;

    public static Restarter getInstance() {
        synchronized (INSTANCE_MONITOR) {
//            Assert.state(instance != null, "Restarter has not been initialized");\
            if(instance==null)instance = new Restarter();
            return instance;
        }
    }

    public  boolean reboot() {
        try {
            LOGGER.info("reboot");

//            String apphome = JarTool.getJarPath().substring(0, JarTool.getJarPath().indexOf("file"));

            if (isOSWindows()) {
                LOGGER.info("Windows");
            } else {
                LOGGER.info("linux");
                // exeCmd = "/bin/sh -c startWeb.sh";
                // String command = "find " + source.getRoute() + " -name '" + source.getName();
                // spring-boot restart /usr/local/quickpf/config_client/spring-config-client-0.0.1.jar

//                String shpath = apphome + "restart.sh";
                // LOGGER.info(shpath);
                // Process process = Runtime.getRuntime().exec(shpath);
                //
                // // Process process = Runtime.getRuntime().exec("spring-boot restart " + apphome + appname);
                // appname = "spring-config-client-0.0.1.jar";
                //
                // LOGGER.info(apphome + appname);
                //
                // // Process process =
                // // Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", "spring-boot restart " +apphome+
                // // appname});
                //
                // printMessage(process.getInputStream());
                // printMessage(process.getErrorStream());
                // int value = process.waitFor();
                // LOGGER.info(String.valueOf(value));
            }
        } catch (Exception e) {
            LOGGER.error("reboot", e);
            e.printStackTrace();
        }

        return false;
    }

    private static void printMessage(final InputStream input) {
        new Thread(new Runnable() {
            public void run() {
                //
                Reader reader = new InputStreamReader(input);
                BufferedReader bf = new BufferedReader(reader);
                String line = null;
                try {
                    while ((line = bf.readLine()) != null) {
                        System.out.println(line);
                        LOGGER.info(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static boolean isOSWindows() {
        String osName = System.getProperty("os.name");
        if (Strings.isNullOrEmpty(osName)) {
            return false;
        }
        return osName.startsWith("Windows");
    }

}
